#include "globalobserver.h"

GlobalObserver GlobalObserver::instance;

GlobalObserver::GlobalObserver(QObject *parent) : QObject(parent)
{

}

GlobalObserver::~GlobalObserver()
{
    //释放列表数据
    QList<RelationData*>::iterator iter = m_relation_list.begin();

    while (iter != m_relation_list.end())
    {
        delete (*iter)->observer_apater;
        delete *iter;
        iter++;
    }
}

GlobalObserver* GlobalObserver::getGlobalObserver()
{
    return &instance;
}

void GlobalObserver::attach(ObserverMsgType msg_type, QObject *reciver, const char *method)
{
    ObserverApater *oa = new ObserverApater();
    switch (msg_type)
    {
        case loss_select_msg:
            connect(oa, SIGNAL(notify()), reciver, method);
            break;
        case item_select_msg:
            connect(oa, SIGNAL(notify(int, int, int, int, QString)), reciver, method);
            break;
        case layout_select_msg:
            connect(oa, SIGNAL(notify(int, int, int)), reciver, method);
            break;
        case partition_select_msg:
            connect(oa, SIGNAL(notify(int, int)), reciver, method);
        break; 
        case screen_select_msg:
            connect(oa, SIGNAL(notify(int)), reciver, method);
        break;
        
        default:
            break;
    }
    RelationData *data = new RelationData();
    data->msg_type = msg_type;
    data->reciver = reciver;
    data->observer_apater = oa;
    m_relation_list.append(data);
}

void GlobalObserver::detach(ObserverMsgType msg_type, const QObject *reciver)
{
    QList<RelationData*>::iterator iter = m_relation_list.begin();

    while (iter != m_relation_list.end())
    {
        if ((*iter)->msg_type == msg_type && (*iter)->reciver == reciver)
        {
            RelationData *data = *iter;
            m_relation_list.removeOne((*iter));

            delete data->observer_apater;
            delete data;
            return;
        }
        iter++;
    }
}

void GlobalObserver::notify(ObserverMsgType msg_type)
{
    QList<RelationData*>::iterator iter = m_relation_list.begin();
    while (iter != m_relation_list.end())
    {
        if ((*iter)->msg_type == msg_type)
            emit (*iter)->observer_apater->notify();

        iter++;
    }
}
void GlobalObserver::notify(ObserverMsgType msg_type, int screen_id, int partition_id, int layout_id, int item_id, QString cab_pos_item_str)
{
    QList<RelationData*>::iterator iter = m_relation_list.begin();
    while (iter != m_relation_list.end())
    {
        if ((*iter)->msg_type == msg_type)
            emit (*iter)->observer_apater->notify(screen_id, partition_id, layout_id, item_id, cab_pos_item_str);

        iter++;
    }
}
void GlobalObserver::notify(ObserverMsgType msg_type, int screen_id, int partition_id, int layout_id)
{
    QList<RelationData*>::iterator iter = m_relation_list.begin();
    while (iter != m_relation_list.end())
    {
        if ((*iter)->msg_type == msg_type)
            emit (*iter)->observer_apater->notify(screen_id, partition_id, layout_id);

        iter++;
    }
}
void GlobalObserver::notify(ObserverMsgType msg_type, int screen_id, int partition_id)
{
    QList<RelationData*>::iterator iter = m_relation_list.begin();
    while (iter != m_relation_list.end())
    {
        if ((*iter)->msg_type == msg_type)
            emit (*iter)->observer_apater->notify(screen_id, partition_id);

        iter++;
    }
}
void GlobalObserver::notify(ObserverMsgType msg_type, int screen_id)
{
    QList<RelationData*>::iterator iter = m_relation_list.begin();
    while (iter != m_relation_list.end())
    {
        if ((*iter)->msg_type == msg_type)
            emit (*iter)->observer_apater->notify(screen_id);

        iter++;
    }
}
